﻿Imports System.Runtime.CompilerServices
Imports System.Collections.ObjectModel

<DebuggerDisplay("Production({DebugName})")> _
Public Class NonTerminal
    Inherits ProductionSymbol

    Private ReadOnly m_grammar As Grammar

    Private m_productions As List(Of Production)
    Public ReadOnly Property Productions() As ReadOnlyCollection(Of Production)
        Get
            Return New ReadOnlyCollection(Of Production)(m_productions)
        End Get
    End Property
    Private m_isNullable As Boolean = False

    Friend Sub SetNullable(ByVal nullable As Boolean)
        m_isNullable = nullable
    End Sub

    Public Overrides ReadOnly Property IsNullable() As Boolean
        Get
            Return m_isNullable
        End Get
    End Property

    Friend Sub New(ByVal index As Integer, ByVal grammar As Grammar)
        MyBase.New(index)

        Debug.Assert(grammar IsNot Nothing)
        m_grammar = grammar

        m_productions = New List(Of Production)
        m_first = New HashSet(Of Terminal)
    End Sub

    Public Sub Assign(ByVal sequence As Sequence)
        m_productions.Add(New Production(sequence, Me))
    End Sub

    Public Function Produces(ByVal ParamArray symbols As ProductionSymbol()) As Production
        Dim result = New Production(New Sequence(symbols), Me)
        m_productions.Add(result)

        'register to the production manager
        m_grammar.AddProduction(result)

        Return result
    End Function

    Private m_first As HashSet(Of Terminal)

    Friend Function GetFirstSet() As HashSet(Of Terminal)
        Return m_first
    End Function

    Public Overrides ReadOnly Property First() As IEnumerable(Of Terminal)
        Get
            Return m_first
        End Get
    End Property

End Class

